package com.gao.mongo.controller;

import com.gao.mongo.dao.CartRepository;
import com.gao.mongo.domain.Cart;
import com.gao.mongo.vo.CartPageVo;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 * ClassName: CartController
 * Description:
 * date: 2021/8/27 22:05
 *
 * @author gaoxi
 * @since JDK 1.8
 */
@Slf4j
@RestController
public class CartController {

    //======================jpa============================================
    @Resource
    private CartRepository cartRepository;


    @GetMapping("/queryCartList")
    public CartPageVo queryCartList(Integer userId,
                                    Integer pageNum,
                                    Integer pageSize) {
        Sort sortParam = Sort.by("createAt").descending();
        Page<Cart> cartListPageByUserId = cartRepository.findCartListPageByUserId(userId,
                PageRequest.of(pageNum, pageSize, sortParam));
        CartPageVo cartPageVo = new CartPageVo();
        cartPageVo.setTotal(cartListPageByUserId.getTotalElements());
        cartPageVo.setResults(cartListPageByUserId.getContent());
        return cartPageVo;
    }


    @PostMapping("/addCart")
    public String addCart(@RequestBody Cart param) {
        param.setCreateAt(new Date());
        Cart result = cartRepository.insert(param);
        log.info("添加后：{}", result);
        return result.getId();
    }

    @GetMapping("/deleteCart")
    public Boolean deleteCart(String[] cartIds) {
        cartRepository.deleteAllById(Arrays.asList(cartIds));
        return true;
    }

    // ===================通过mongoTemplate操作==============================
    @Resource
    private MongoTemplate mongoTemplate;

    @GetMapping("addCartNum")
    public Boolean addCartNum(String cartId, String op) {
        Query query = new Query(Criteria.where("_id").is(cartId));
        Update update;
        if (op.equals("1")) {
            //+1
            update = new Update().inc("num", 1);
        } else {
            //-1
            update = new Update().inc("num", -1);
        }
        UpdateResult cart = mongoTemplate.updateFirst(query, update, "cart");
        return cart.getMatchedCount() == 1;
    }


    @GetMapping("modifyCartNum")
    public Boolean modifyCartNum(String cartId, Integer num) {
        Update update = new Update().set("num", num);
        Query query = new Query(Criteria.where("_id").is(cartId));
        UpdateResult cart = mongoTemplate.updateFirst(query, update, "cart");
        return cart.getMatchedCount() == 1;
    }


}
